package com.jiji.miaosha.util;

import com.alibaba.fastjson.JSON;

public class JsonUtil {

    public static <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        if (value.getClass() == int.class || value.getClass() == Integer.class) {
            return "" + value;
        } else if (value.getClass() == long.class || value.getClass() == Long.class) {
            return "" + value;
        } else if (value.getClass() == String.class) {
            return (String) value;
        }
        return JSON.toJSONString(value);
    }

    public static <T> T stringToBean(String str, Class<T> clz) {
        if (str == null || str.length() <= 0 || clz == null) {
            return null;
        }
        if (clz == int.class || clz == Integer.class) {
            return (T) Integer.valueOf(str);
        } else if (clz == long.class || clz == Long.class) {
            return (T) Long.valueOf(str);
        } else if (clz == String.class) {
            return (T) str;
        }
        return JSON.toJavaObject(JSON.parseObject(str), clz);
    }
}
